<?php
namespace app\api\controller;

use app\api\annotation\LoginRequired;
use app\api\basic\BaseController;
use app\api\logic\CollectionLogic;
use app\enterpriseContact\logic\enterpriseContactLogic;
use support\Request;
use app\supplyDemand\logic\supplyDemandLogic;
class SupplyController extends BaseController
{
     protected $userInfo;

    /**
     * 当前登陆管理员ID
     */
    protected $userId;

    /**
     * 当前登陆管理员账号
     */
    protected $userName;
    public function __construct()
    {   
        parent::__construct();
        $this->userId=request()->userId;
        $this->userName=request()->userName;
        $this->userInfo=request()->userInfo;
    }
   /**
     * 供求/列表
     */
    #[LoginRequired(required: false)]
    public function lists(Request $request){
       
        $param = $request->more([
            ['lang_code','zh'],
            ['limit',10],
            ['page',1],
            ['product_name',''],
            ['product_category',''],
            ['supply_demand','']
        ]);

        $user_id =$this->userId;
        $type =2;
        $supplyDemandData=productRole($user_id,$type,$param['page'],$param['limit'],$param);
       // print_r($supplyDemandData);
//        $ebSupplyDemand=new supplyDemandLogic();
//        $query=$ebSupplyDemand->search([
//            'lang_code'=>$param['lang_code'],
//         //   'product_name'=>$param['product_name'],
//            'product_category'=>$param['product_category'],
//            'supply_demand'=>$param['supply_demand'],
//        ]);
//        if($param['product_name']){
//            $query->where('product_name|product_subtitle|supply_demand_datail', 'like', "%{$param['product_name']}%");
//        }
//        $supplyDemandData=$query
//        ->field('sd_id,product_name,product_subtitle,image,supply_demand,product_category')
//            ->order('sort_order desc')
//        ->order('create_time desc')
//        ->paginate($param['limit']);
        //return $this->success(['data'=>$supplyDemandData->items(),'total'=>$supplyDemandData->total()]);
        return $this->success(['data'=>$supplyDemandData['data'],'total'=>$supplyDemandData['pagination']['total']]);
    }
       #[LoginRequired(required: false)]
      public function detail(Request $request){
       
        $param = $request->more([
            ['sd_id',''],
        ]);
        if(!$param['sd_id'])
        {
            return $this->fail('缺少参数');
        }
        $ebSupplyDemand=new supplyDemandLogic();
        $enterpriseContactLogic=new enterpriseContactLogic();
        $collectionLogic=new CollectionLogic();
        $supplyDemandData=$ebSupplyDemand->search(['sd_id'=>$param['sd_id']])
        ->field('sd_id,product_name,product_subtitle,image,supply_demand,product_category,supply_demand_datail,enterprise_id')
        ->order('create_time desc')
        ->find();
        $data=[];
        if($supplyDemandData){
            $recruitmentEnterpriseData=$enterpriseContactLogic->field(
                'enterprise_name,contact_person,contact_number')
            ->find($supplyDemandData->enterprise_id);
            $data=$supplyDemandData->toArray();
             $data['enterprise']=[];
            if($recruitmentEnterpriseData){
                $data['enterprise']=$recruitmentEnterpriseData->toArray();
            }
            //获取是否收藏
            $data['is_collection']=0;
            $collectionRes=$collectionLogic->where(['type'=>2,'collection_id'=>$param['sd_id'],'user_id'=>$this->userId])->findOrEmpty();
            if(!$collectionRes->isEmpty())$data['is_collection']=1;
            
            //记录日志
            $userId =empty($this->userId)?65:$this->userId;
            $result =supplyLog($userId, $data, 1);
            //print_r($result);
            return $this->success($data);

           
        }
        return $this->fail('数据不存在');
             
    }
}